/*
 * adpara.c
 *
 *  Created on: Jul 17, 2021
 *      Author: 本地账户
 */


#include "adpara.h"

//调参函数
//返回结构体：从键盘输入的整数与浮点数
void adpara(ADPARARE* ADP_Struct)
{
	u8 mkv;           //保存键值
	u8 cnt;
	u8 count;
	u8 posym=0;
	int flag;
	u8 mkinput[10];

begin:
	cnt=0;
	posym=0;
	flag=0;
	ADP_Struct->adrint=0;
	ADP_Struct->adrflo=0;
	ADP_Struct->state=0;
	for (count=0;count<10;count++)
	{
		mkinput[count]=0;
	 }
	 //OLED_ShowString(0,24,"INPUT:",16);
	 OLED_Clear();
	 OLED_DisplayString(0,0,16,16,"INPUT");
	 //OLED_Refresh_Screen();
	 do
	 {
		 mkv=RemoteWait();
		 HAL_UART_Transmit(&huart1,"ADP FUNC",strlen("ADP FUNC"),100);

		if (mkv!=KB_UP && mkv!=KB_DOWN && mkv!=KB_OK && mkv!=KB_CLEAR && mkv!=KB_BACK && mkv!=KB_POINT && mkv!=KB_RIGHT && mkv!=KB_LEFT)
		{
			if (posym)
			{
				flag=OLED_ShowKey((cnt+1)*8,2,12,mkv);
			}
			else
			{
				flag=OLED_ShowKey(cnt*8,2,12,mkv);
			}
			//OLED_Refresh_Screen();
			mkinput[cnt++]=flag;
		 }
		 else if (mkv==KB_POINT)
		 {
				OLED_ShowKey(cnt*8,2,12,mkv);
				//OLED_Refresh_Screen();
				posym=cnt;
		 }
         else if(mkv==KB_BACK)
		 {
			 goto begin;
		 }
         else if(mkv==KB_OK)
		 {
			ADP_Struct->adrint = 0;
			for (count=0;count<cnt;count++)
			{
				ADP_Struct->adrint += mkinput[count]*(int)(pow(10,cnt-1-count)+(1E-6));
			}

			if (posym)
			{
				ADP_Struct-> adrflo = ADP_Struct-> adrint/pow(10,cnt-posym);
			}
			else
			{
				ADP_Struct-> adrflo = ADP_Struct-> adrint;
			}

		}
	 }while(mkv!=KB_OK);

	 if (mkv==KB_OK)
	 {

		 OLED_Clear();

		 //OLED_ShowString(0,0,"Done",16);
		 OLED_DisplayString(0,0,16,16,"Done");
		 //OLED_Refresh_Screen();

		 mkv=RemoteWait();
		 if (mkv==KB_OK)
		 {
			 ADP_Struct->state=STA_MAIN;
		 }
		 else if (mkv==KB_BACK)
		 {
			 ADP_Struct->state=STA_PREV;
		 }
	 }
}








